﻿using Ardalis.GuardClauses;
using CleanArchitecture.Core.Configuration;
using CleanArchitecture.Core.Events;
using CleanArchitecture.Core.Interfaces;
using CleanArchitecture.Infrastructure.Entities.AdminUsers;
using CleanArchitecture.Infrastructure.Entities.Business;
using CleanArchitecture.Infrastructure.Interfaces.SystemLog;
using CleanArchitecture.Infrastructure.Services.Media;

namespace CleanArchitecture.Web.DomainEventsHandler
{
    public class SystemLoggerEventHandler : 
        //AdminUser
        IHandle<EntityUpdatedEvent<AdminUser>>,
        IHandle<AdminUserLoggedEvent>,
        //Picture
        IHandle<QiniuCloudHttpEvent>,
        IHandle<EntityInsertedEvent<FileUpload>>,
        IHandle<EntityUpdatedEvent<FileUpload>>,
        IHandle<EntityDeletedEvent<FileUpload>>,
        //Business Column
        IHandle<EntityInsertedEvent<Column>>,
        IHandle<EntityUpdatedEvent<Column>>,
        IHandle<EntityDeletedEvent<Column>>,
        //Business News
        IHandle<EntityInsertedEvent<NewsItem>>,
        IHandle<EntityUpdatedEvent<NewsItem>>,
        IHandle<EntityDeletedEvent<NewsItem>>,
        //Business Advertise
        IHandle<EntityInsertedEvent<Advertise>>,
        IHandle<EntityUpdatedEvent<Advertise>>,
        IHandle<EntityDeletedEvent<Advertise>>,
        //Business Comment
        IHandle<EntityInsertedEvent<Comment>>,
        IHandle<EntityUpdatedEvent<Comment>>,
        IHandle<EntityDeletedEvent<Comment>>
    {

        #region Fields

        private readonly ISystemLogService _loggerService;

        #endregion

        #region Ctor

        public SystemLoggerEventHandler(ISystemLogService loggerService)
        {
            this._loggerService = loggerService;
        }

        #endregion

        #region Utility
        private void LogEvent(SystemLogLevel level, string logmsg)
        {
            if (!string.IsNullOrEmpty(logmsg))
            {
                switch (level)
                {
                    case SystemLogLevel.Information:
                        _loggerService.Information(logmsg);
                        break;
                    case SystemLogLevel.Warning:
                        _loggerService.Warning(logmsg);
                        break;
                    case SystemLogLevel.Error:
                        _loggerService.Error(logmsg);
                        break;
                    default:
                        _loggerService.Debug(logmsg);
                        break;
                }
            }
        }
        #endregion

        #region AdminUser
        public void Handle(AdminUserLoggedEvent domainEvent)
        {
            Guard.Against.Null(domainEvent, nameof(domainEvent));
            LogEvent(domainEvent.Entity.SysLogLvl, domainEvent.Entity.SysLogMsg);
        }
       
        public void Handle(EntityUpdatedEvent<AdminUser> domainEvent)
        {
            Guard.Against.Null(domainEvent, nameof(domainEvent));
            LogEvent(domainEvent.Entity.SysLogLvl, domainEvent.Entity.SysLogMsg);
        }
        #endregion

        #region Column
        public void Handle(EntityInsertedEvent<Column> domainEvent)
        {
            Guard.Against.Null(domainEvent, nameof(domainEvent));
            LogEvent(domainEvent.Entity.SysLogLvl, domainEvent.Entity.SysLogMsg);
        }
        public void Handle(EntityUpdatedEvent<Column> domainEvent)
        {
            Guard.Against.Null(domainEvent, nameof(domainEvent));
            LogEvent(domainEvent.Entity.SysLogLvl, domainEvent.Entity.SysLogMsg);
        }
        public void Handle(EntityDeletedEvent<Column> domainEvent)
        {
            Guard.Against.Null(domainEvent, nameof(domainEvent));
            LogEvent(domainEvent.Entity.SysLogLvl, domainEvent.Entity.SysLogMsg);
        }
        #endregion

        #region News
        public void Handle(EntityInsertedEvent<NewsItem> domainEvent)
        {
            Guard.Against.Null(domainEvent, nameof(domainEvent));
            LogEvent(domainEvent.Entity.SysLogLvl, domainEvent.Entity.SysLogMsg);
        }
        public void Handle(EntityUpdatedEvent<NewsItem> domainEvent)
        {
            Guard.Against.Null(domainEvent, nameof(domainEvent));
            LogEvent(domainEvent.Entity.SysLogLvl, domainEvent.Entity.SysLogMsg);
        }
        public void Handle(EntityDeletedEvent<NewsItem> domainEvent)
        {
            Guard.Against.Null(domainEvent, nameof(domainEvent));
            LogEvent(domainEvent.Entity.SysLogLvl, domainEvent.Entity.SysLogMsg);
        }
        #endregion

        #region FileUpload
        public void Handle(QiniuCloudHttpEvent domainEvent)
        {
            Guard.Against.Null(domainEvent, nameof(domainEvent));
            LogEvent(domainEvent.Entity.SysLogLvl, domainEvent.Entity.SysLogMsg);
        }

        public void Handle(EntityInsertedEvent<FileUpload> domainEvent)
        {
            Guard.Against.Null(domainEvent, nameof(domainEvent));
            LogEvent(domainEvent.Entity.SysLogLvl, domainEvent.Entity.SysLogMsg);
        }

        public void Handle(EntityUpdatedEvent<FileUpload> domainEvent)
        {
            Guard.Against.Null(domainEvent, nameof(domainEvent));
            LogEvent(domainEvent.Entity.SysLogLvl, domainEvent.Entity.SysLogMsg);
        }

        public void Handle(EntityDeletedEvent<FileUpload> domainEvent)
        {
            Guard.Against.Null(domainEvent, nameof(domainEvent));
            LogEvent(domainEvent.Entity.SysLogLvl, domainEvent.Entity.SysLogMsg);
        }
        #endregion

        #region Advertise
        public void Handle(EntityInsertedEvent<Advertise> domainEvent)
        {
            Guard.Against.Null(domainEvent, nameof(domainEvent));
            LogEvent(domainEvent.Entity.SysLogLvl, domainEvent.Entity.SysLogMsg);
        }
        public void Handle(EntityUpdatedEvent<Advertise> domainEvent)
        {
            Guard.Against.Null(domainEvent, nameof(domainEvent));
            LogEvent(domainEvent.Entity.SysLogLvl, domainEvent.Entity.SysLogMsg);
        }
        public void Handle(EntityDeletedEvent<Advertise> domainEvent)
        {
            Guard.Against.Null(domainEvent, nameof(domainEvent));
            LogEvent(domainEvent.Entity.SysLogLvl, domainEvent.Entity.SysLogMsg);
        }
        #endregion
     
        #region Comment
        public void Handle(EntityInsertedEvent<Comment> domainEvent)
        {
            Guard.Against.Null(domainEvent, nameof(domainEvent));
            LogEvent(domainEvent.Entity.SysLogLvl, domainEvent.Entity.SysLogMsg);
        }
        public void Handle(EntityUpdatedEvent<Comment> domainEvent)
        {
            Guard.Against.Null(domainEvent, nameof(domainEvent));
            LogEvent(domainEvent.Entity.SysLogLvl, domainEvent.Entity.SysLogMsg);
        }
        public void Handle(EntityDeletedEvent<Comment> domainEvent)
        {
            Guard.Against.Null(domainEvent, nameof(domainEvent));
            LogEvent(domainEvent.Entity.SysLogLvl, domainEvent.Entity.SysLogMsg);
        }
        #endregion

    }
}
